
package cn.edu.lzu.dao;

import cn.edu.lzu.entity.BookRegiste;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.User;
import org.apache.ibatis.annotations.*;

import java.awt.print.Book;
import java.sql.Date;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.dao
 * @InterfaceName: BookRegiste
 * @Description: TODO 对book_registe表的操作
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/2/27 16:31
 * @Version: 1.0
 */
@Mapper
public interface BookRegisteMapper {
    @Insert("insert into book_registe(id, user_identity, user_status, book_date, is_deleted, examine_package_id, examiner_id, is_charged) values(default, #{userIdentity}, #{userStatus}, #{bookDate}, #{isDeleted}, #{examinePackageId}, #{examinerId}, 0)")
    int insRegisteMsg(BookRegiste bookRegiste);

    @Insert("insert into book_registe(id, user_identity, user_status, book_date, is_deleted, examine_package_id, examiner_id, is_charged) values(default, #{bookRegiste.userIdentity}, 2, #{bookRegiste.bookDate}, #{bookRegiste.isDeleted}, #{bookRegiste.examinePackageId}, #{unitId}, 0)")
    int insBookRegisteMsg(@Param("bookRegiste") BookRegiste bookRegiste, @Param("unitId") long unitId);

    @Select("select * from book_registe where is_deleted=0 and examiner_id in (select examiner_id from examiner where unit_id=#{examiner.unitId}) and user_status in (1, 2) and user_identity=#{user.userIdentity}")
    List<BookRegiste> selRegisteMsgRegisting(@Param("examiner") Examiner examiner, @Param("user") User user);

    @Select("select * from book_registe where is_deleted=0 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status in (1, 2) and user_identity=#{user.userIdentity}")
    List<BookRegiste> selRegisteMsgRegisted(@Param("unitId") long unitId, @Param("user") User user);

    @Select("select * from book_registe where is_deleted=0 and examiner_id=#{unitId} and user_status in (1, 2) and user_identity=#{user.userIdentity}")
    List<BookRegiste> selBookedMsg(@Param("user") User user, @Param("unitId") long unitId);

    //获取一页挂号用户列表
    @Select("select * from book_registe where is_deleted=0 and user_status=1 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) order by id DESC limit #{startIndex}, #{pageSize} ")
    List<BookRegiste> selPageRegisteUserList(int startIndex, int pageSize, long unitId);

    //获取一页挂号预约信息列表
    @Select("select * from book_registe where is_deleted=0 and user_status in(1, 2) and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) order by id DESC limit #{startIndex}, #{pageSize}")
    List<BookRegiste> selPageAllUserList(int startIndex, int pageSize, long unitId);

    //获取已缴费bookRegiste记录列表
    @Select("select * from book_registe where is_deleted=0 and is_charged=1 and user_status in(1, 2) and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) order by id DESC limit #{startIndex}, #{pageSize}")
    List<BookRegiste> selChangedUserList(int startIndex, int pageSize, long unitId);

    //获取未缴费bookRegiste记录列表
    @Select("select * from book_registe where is_deleted=0 and is_charged=0 and user_status in(1, 2) and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) order by id DESC limit #{startIndex}, #{pageSize}")
    List<BookRegiste> selUnchangedUserList(int startIndex, int pageSize, long unitId);

   //获取一页挂号预约信息列表
    @Select("select count(*) from book_registe where is_deleted=0 and user_status in(1, 2) and examiner_id in (select examiner_id from examiner where unit_id=#{unitId})")
    int selAllUserChargeMsgCount(long unitId);

    //已缴费用户数量
    @Select("select count(*) from book_registe where is_deleted=0 and is_charged=1 and user_status in(1, 2) and examiner_id in (select examiner_id from examiner where unit_id=#{unitId})")
    int selChargedUserChargeMsgCount(long unitId);

    //未缴费用户数量
    @Select("select count(*) from book_registe where is_deleted=0 and is_charged=0 and user_status in(1, 2) and examiner_id in (select examiner_id from examiner where unit_id=#{unitId})")
    int selUnchargedUserChargeMsgCount(long unitId);

    //获取挂号总数
    @Select("select count(*) from book_registe where is_deleted=0 and user_status=1 and  examiner_id in (select examiner_id from examiner where unit_id=#{unitId})")
    int selCountRegiste(long unitId);

    //挂号页面根据userIdentity的模糊搜索
    @Select("select * from book_registe where is_deleted=0 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status=1 and user_identity like concat('%', ${userIdentity}, '%') limit 0, #{pageSize}")
    List<BookRegiste> selFuzzyByUserIdentity(long unitId, String userIdentity, int pageSize);

    //所有用户缴费页面根据userIdentity的模糊搜索
    @Select("select * from book_registe where is_deleted=0 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status in(1, 2) and user_identity like concat('%', ${userIdentity}, '%') limit 0, #{pageSize}")
    List<BookRegiste> selFuzzyAllUserChargeMsgByUserIdentity(long unitId, String userIdentity, int pageSize);

    //已缴费用户缴费页面根据userIdentity的模糊搜索
    @Select("select * from book_registe where is_deleted=0 and is_charged=1 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status in(1, 2) and user_identity like concat('%', ${userIdentity}, '%') limit 0, #{pageSize}")
    List<BookRegiste> selFuzzyChargedUserChargeMsgByUserIdentity(long unitId, String userIdentity, int pageSize);

    //未缴费用户缴费页面根据userIdentity的模糊搜索
    @Select("select * from book_registe where is_deleted=0 and is_charged=0 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status in(1, 2) and user_identity like concat('%', ${userIdentity}, '%') limit 0, #{pageSize}")
    List<BookRegiste> selFuzzyUnchargedUserChargeMsgByUserIdentity(long unitId, String userIdentity, int pageSize);

    //根据userIdentity的模糊搜索获取totalCount
    @Select("select count(*) from book_registe where is_deleted=0 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status=1 and user_identity like concat('%', ${userIdentity}, '%')")
    int selFuzzyCountByUserIdentity(long unitId, String userIdentity);

    //所有用户缴费页面根据userIdentity的模糊搜索获取totalCount
    @Select("select count(*) from book_registe where is_deleted=0 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status in(1, 2) and user_identity like concat('%', ${userIdentity}, '%')")
    int selFuzzyAllUserChargeMsgCountByUserIdentity(long unitId, String userIdentity);

    //缴费用户缴费页面根据userIdentity的模糊搜索获取totalCount
    @Select("select count(*) from book_registe where is_deleted=0 and is_charged=1 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status in(1, 2) and user_identity like concat('%', ${userIdentity}, '%')")
    int selFuzzyChargedUserChargeMsgCountByUserIdentity(long unitId, String userIdentity);

    //未缴费用户缴费页面根据userIdentity的模糊搜索获取totalCount
    @Select("select count(*) from book_registe where is_deleted=0 and is_charged=0 and examiner_id in (select examiner_id from examiner where unit_id=#{unitId}) and user_status in(1, 2) and user_identity like concat('%', ${userIdentity}, '%')")
    int selFuzzyUnchangedUserChargeMsgCountByUserIdentity(long unitId, String userIdentity);

    //更新缴费状态为“已缴费”
    @Update("update book_registe set is_charged=1 where id=#{id} and is_deleted=0")
    public int updSetChargedTrue(int id);

    //根据id获取bookRegiste
    @Select("select * from book_registe where id=#{id} and is_deleted=0")
    public BookRegiste selBookRegisteById(int id);

    @Select("select id from book_registe where book_date=#{bookDate} and user_identity=#{userIdentity}")
    int selByDateAndUserIdentity(Date bookDate, String userIdentity);

    @Update("update book_registe set user_status=#{userStatus} where id=#{bookRegisteId}")
    int updUserStatus(int userStatus, int bookRegisteId);

    @Select("select * from book_registe where is_deleted=0 and user_status=2 and examiner_id=#{unitId} order by id DESC limit #{startIndex}, #{pageSize} ")
    List<BookRegiste> selBookedUser(int startIndex, Integer pageSize, long unitId);

    @Select("select count(*) from book_registe where is_deleted=0 and user_status=2 and  examiner_id=#{unitId}")
    int selCountBooked(long unitId);
}
